/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Icon;

import fever.data.validators.ValidationResources;

import fvaswing.components.forms.FvFormField;
import fvaswing.components.FvCalendarField;

/**
 * Calendar form field.
 * 
 * <p>Allow user to place a calendar chooser field into form.<br />
 * 
 * <p>Example
 * {@code
 *   var field : FvDateField = new FvDateField( "Birthday", true );
 *   dateField.setMinimumYear( 2000 );
 *   dateField.setMaximumYear( 2006 );
 * }
 *  
 * <p>If you don't want to use form feature, take a look at 
 * {@link fvaswing.components.FvCalendarField} simple component.
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.forms.FvDateField extends FvFormField
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
		private var _input : FvCalendarField;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param label Field caption
	 * @param minLength Minimum length for password value
	 * @param maxLength Maximum length for password value
	 */
	public function FvDateField( label : String, required : Boolean )
	{
		super( label );
		
		_isValidationEnabled = ( required == true ) ? true : false;
		
		_createValueComponent();
	}
	
	/**
	 * Sets field value.
	 */
	public function setValue( date : Date ) : Void
	{
		super.setValue( date );
	}
	
	/**
	 * Returns field value.
	 */
	public function getValue( ) : Date
	{
		return _input.getValue();
	}
	
	/**
	 * Clears field value.
	 */
	public function clear() : Void
	{
		_input.clear();
	}
	
	/**
	 * Sets max year value.
	 */
	public function setMaximumYear( year : Number ) : Void
	{
		_input.setMaximumYear( year );
	}
	
	/**
	 * Returns max year value.
	 */
	public function getMaximumYear() : Number
	{
		return _input.getMaximumYear();	
	}
	
	/**
	 * Sets min year value.
	 */
	public function setMinimumYear( year : Number ) : Void
	{
		_input.setMinimumYear( year );	
	}
	
	/**
	 * Returns min year value.
	 */
	public function getMinimumYear() : Number
	{
		return _input.getMinimumYear();		
	}
	
	/**
	 * Indicates if component highlight 'today' cell.<br />
	 * Default is {@code true}
	 */
	public function setShowTodayEnabled( b : Boolean ) : Void
	{
		_input.setShowTodayEnabled( b );
	}
	
	/**
	 * Returns {@code true} if calendar highlight the 'today' cell.
	 */
	public function isShowTodayEnabled() : Boolean
	{
		return _input.isShowTodayEnabled();	
	}
	
	/**
	 * Sets the formatting pattern to use when receive date from 
	 * {@link fvaswing.components.FvCalendar} component.
	 * 
	 * <p>Default is connected to the <strong>Localisation API</strong>
	 */
	public function setFormatPattern( pattern : String ) : Void
	{
		_input.setFormatPattern( pattern );
	}
	
	/**
	 * Returns used formatting pattern.
	 */
	public function getFormatPattern() : String
	{
		return _input.getFormatPattern();	
	}
	
	/**
	 * Sets button icon.
	 */
	public function setIcon( icon : Icon ) : Void
	{
		_input.setIcon( icon );	
	}
	
	/**
	 * Returns button icon.
	 */
	public function getIcon() : Icon
	{
		return _input.getIcon();
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Returns component dedicated to field value.
	 */
	private function _createValueComponent() : FvCalendarField
	{
		if( !_input ) _input = new FvCalendarField();
		
		return _input;
	}
	
	private function _initWithData( date : Date ) : Void
	{
		_input.setValue( date );	
	}
	
	/**
	 * Process validation
	 */
	private function _validateField() : Boolean
	{
		return _triggerResult( _input.getValue() != undefined, ValidationResources.getInstance().required_field_error );
	}
}